<?php
/**
 * 下载远程图片
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(2);

$limit = 500;

while($limit--) {
    var_dump($limit);
    $org_data = $redis->lPop('se126_image_key');

    if(!$org_data) {
        sleep(2);
        continue;
    }
    //$redis->rPush('se126_image_key', $org_data);

    $data = unserialize($org_data);
    $md5  = $data['md5'];
    unset($data['md5']);

    $data['code'] = '0623';
    $http_query_data  = http_build_query($data);

    if($md5 != md5($http_query_data)) {
        var_dump($md5, $http_query_data);
        continue;
    }

    $base_dir  = '/home/wwwroot/dl.se126.com/thumb/';
    $savename  = se_get_savename($data['post_id'], $data['src'], $data['act']);
    $save_path = $base_dir.$savename;

    if(file_exists($save_path)) {
        echo 'aleady exists' . "\r\n";
        continue;
    }

    if(stristr($data['src'], 'upaiyun') || stristr($data['src'], 'se126.com/wp-content/themes')) {
        echo 'url ignore' . "\r\n";
        continue;
    }

    //下载远程图片
    $cmd  = 'cd '.$base_dir.';';
    $cmd .= 'wget -O '.$savename.' --limit-rate=1000k --continue --timeout=300 --tries=5 --no-clobber --debug --force-directories --no-host-directories '.$data['src'];
    shell_exec($cmd);

    if(file_exists($save_path)) {
        echo 'success' . "\r\n";

        //生成缩略图
        if(!strstr($savename, '_thumb')) {
            $org_savename   = $savename;
            $thumb_savename = se_get_savename($data['post_id'], $data['src'], 'thumb');
        } else {
            $org_savename   = str_replace('_thumb', '', $savename);
            $thumb_savename = $savename;

            copy($save_path, $base_dir . $org_savename);
        }

        thumb_image($save_path, $base_dir.$thumb_savename);

        //大图重新生成渐进式格式
        image_interlace($base_dir . $org_savename);
    }
    else {
        $redis->rPush('se126_image_key', $org_data);
        echo 'failed' . "\r\n";
    }

}

//格式化本地文件名
function se_get_savename($post_id, $link, $act) {
    $arr = explode('.', $link);
    $ext = end($arr);
    
    if($act == 'thumb') {
        return $post_id.'_'.substr(md5($link), 0, 8).'_thumb.'.$ext;
    }
    else {
        return $post_id.'_'.substr(md5($link), 0, 8).'.'.$ext;
    }
}

//生成缩略图
function thumb_image($filename, $thumb_savename) {

    // Set a maximum height and width
    $width = 400;
    $height = 250;

    // Content type
    //header('Content-type: image/jpeg');

    // Get new dimensions
    list($width_orig, $height_orig) = getimagesize($filename);

    if($height_orig > 650) {
        $src_y = 400;
    } else {
        $src_y = 0;
    }

    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);

    imagecopyresampled($image_p, $image, 0, 0, 0, $src_y, $width, $height, $width_orig, $height);
    imageinterlace($image_p, true);

    // Output
    return imagejpeg($image_p, $thumb_savename, 90);
}

//渐进式
function image_interlace($save_path) {
    $im = imagecreatefromjpeg($save_path);
    imageinterlace($im, true);
    imagejpeg($im, $save_path, 100);
    imagedestroy($im);
}


